import {
  WEB_UPDATE_FILTER_CONDITIONS,
  WEB_UPDATE_TASKS,
  WEB_CHANGE_LOADING,
  WEB_UPDATE_TREND
} from "./reducer";
import {fetchMemorySearchApi, fetchMemoryListInitData, fetchMemoryTrendInitData} from "../../../common/api";

export function fetchInitData() {
  return (dispatch) => {
    return fetchMemoryListInitData()
      .then(data => {
        console.log("fetchInitData");
        console.log(data);
        dispatch({
          type: WEB_UPDATE_TASKS,
          tasks: data
        })
      })
  }
}

export function updateFilterConditions(newFilterConditions) {
  return (dispatch) => {
    dispatch({
      type: WEB_UPDATE_FILTER_CONDITIONS,
      filterConditions: newFilterConditions
    });
  };
}

export function changeLoading(loading) {
  return (dispatch) => {
    dispatch({
      type: WEB_CHANGE_LOADING,
      loading: loading
    });
  };
}

export function fetchTasks({deviceId, platform, version, unionId, beginTime, endTime, pageNum, pageSize}) {
  return (dispatch) => {
    return fetchMemorySearchApi(deviceId, platform, version, unionId, beginTime, endTime, pageNum, pageSize)
      .then(data => {
        dispatch({
          type: WEB_UPDATE_TASKS,
          tasks: data
        });
      });
  };
}

// 获得卡顿趋势图表数据
export function fetchTrendData() {
  return (dispatch) => {
    return fetchMemoryTrendInitData()
      .then(data => {
        console.log(data);
        dispatch({
          type: WEB_UPDATE_TREND,
          trendData: data
        })
      })
  }
}